package com.nexus.service.sys.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nexus.core.exception.ServiceException;
import com.nexus.core.service.TreeService;
import com.nexus.core.sys.entity.SysMenu;
import com.nexus.core.sys.dto.SysMenuDTO;
import com.nexus.service.sys.mapper.SysMenuMapper;
import com.nexus.service.sys.service.mapstruct.SysMenuWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 系统菜单Service
 *
 * @author 路正宁
 * @version 2023-03-21
 */
//@DS("zxhy_platform_db")
//@Service
@Transactional(rollbackFor = {ServiceException.class,Exception.class, RuntimeException.class})
public class SysMenuService extends TreeService<SysMenuMapper, SysMenu, SysMenuDTO,SysMenuWrapper> {

    @Autowired
    SysMenuWrapper sysMenuWrapperImpl;
    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public SysMenuDTO findById(String id) {
        return baseMapper.findById(id);
    }

//    public List<SysMenuDTO> findByIds(List<String> ids){
//        QueryWrapper queryWrapper=new QueryWrapper();
//        queryWrapper.in("",ids);
//    }

    /**
     * 查询全部数据
     *
     * @return
     */
    public List<SysMenuDTO> listDTO() {
        return baseMapper.findList();
    }
    public SysMenuDTO listDTO(SysMenu sysMenu) {
        return sysMenuWrapperImpl.toDTO(sysMenu);
    }

    public boolean saveOrUpdate(SysMenu sysMenu) {
        return super.saveOrUpdate(sysMenu);
    }

    /**
     * 修改排序
     * @author luzhengning
     * @date 2023/3/23 9:41
     */
    public boolean updateSort(String id, int sort,String parentId,String parentIds){
        int i = baseMapper.updateSort(id, sort,parentId,parentIds);
        return i>0;
    }

    public SysMenuDTO saveOrUpdate(SysMenuDTO sysMenuDTO){
        SysMenu entity = sysMenuWrapperImpl.toEntity(sysMenuDTO);
        if(saveOrUpdate(entity)){
            return sysMenuWrapperImpl.toDTO(entity);
        }
        return null;
    }

    public List<SysMenu> findByIds(List<String> ids){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.in("id",ids);
        queryWrapper.eq("deleted","0");
        List<SysMenu> list = list(queryWrapper);
        return list;

    }

}
